package com.infore.siteManagement.repository;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.infore.siteManagement.entity.Item;
import com.infore.siteManagement.vo.ItemNameStationIdVo;
import com.infore.siteManagement.vo.ItemStationVo;

@Repository
public interface IitemRepository extends JpaRepository<Item, String>, JpaSpecificationExecutor<Item> {

	@Modifying
	@Transactional
	int deleteByIdIn(List<String> idsList);

	List<Item> findByNameOrderBySortAsc(String name);

	List<Item> findByNameLikeOrderBySortAsc(String name);

	List<Item> findByItemLevelOrderBySortAsc(String level);

	@Query(value = "select distinct t from Item t left join ItemAndStation ias on ias.itemId = t.id "
			+ "where ias.stationId in :ids order by t.sort ")
	List<Item> queryByStationId(@Param("ids") List<String> ids);

	@Query(value = "select new com.infore.siteManagement.vo.ItemStationVo (t.stationId,t.stationName) from ItemAndStation ias "
			+ "left join Station t on ias.stationId = t.stationId where ias.itemId =:itemId")
	List<ItemStationVo> queryByItemId(@Param("itemId") String itemId);

	@Query(value = "select new com.infore.siteManagement.vo.ItemNameStationIdVo (t.name,ias.stationId,ias.itemId) from ItemAndStation ias left join "
			+ " Item t on t.id = ias.itemId where ias.stationId in (:stationIds) ")
	List<ItemNameStationIdVo> queryItemNameStationId(@Param("stationIds") List<String> stationIds);
}
